<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>09_猜数字练习</title>
</head>
<body>
<h3>已生成一个1-100之间的随机数:</h3>
<input type="text" id="i1" >
<button onclick="test()">点我验证</button>
<h3>小提示:<span id="hint"></span></h3>
<script>
        let result = parseInt(Math.random()*100)+1;
        console.log('小抄:'+result);
        let count = 0;
    function test(){
        let num = parseInt(i1.value);
        //对用户输入的数据进行校验
        if(!/^\d+$/.test(num)){
            alert('请输入数字!')
            return;
        }

        /*if(count == 3){
            alert('你太笨了,猜了3次都猜不着,回去种田吧!');
            return;
        }*/
        count++;
        if (num===result){
            hint.innerHTML ='恭喜你用了'+count+'次就猜对了!';
        }else{
            if (num>result){
                hint.innerHTML = '猜大了';
            }else{
                hint.innerHTML = '猜小了';
            }
        }
    }
</script>
</body>
</html>